package com.starbaba.stat.service.impl;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Component;

import com.base.common.log.LogService;
import com.base.common.util.ObjectUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.model.utils.HttpConnectionUtils;
import com.starbaba.model.utils.JSONUtils;
import com.starbaba.stat.model.ExportResultObject;
import com.starbaba.stat.model.NginxPVUVModel;
import com.starbaba.stat.service.StatService;

@Component
public class StatServiceImpl implements StatService {

	@Override
	public String getExportString(String requestString,String interface_id) throws Exception {
		String url = Constant.COMMON_HOST + "/stat_service/common";
//		String url = "http://localhost:8080/stat_service/common";
		
		// 请求数据
		ArrayList<BasicNameValuePair> pair = new ArrayList<BasicNameValuePair>();
		pair.add(new BasicNameValuePair("data", requestString));
		pair.add(new BasicNameValuePair("shandle", "0"));
		pair.add(new BasicNameValuePair("handle", "0"));
		System.out.println("//////" + ObjectUtils.toJsonString(pair));
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST, pair);
		ExportResultObject resultObject = (ExportResultObject) ObjectUtils.fromJsonToObject(result, ExportResultObject.class);
		String exportString = resultObject.getExportString();
		return exportString;
	}

	@Override
	public String exportString(String request, String interface_id) {
		String url = "http://xmiles.cn/stat_service/common";
		// String url = "http://localhost:8080/stat_service/common";
		// 请求数据
		ArrayList<BasicNameValuePair> pair = new ArrayList<BasicNameValuePair>();
		pair.add(new BasicNameValuePair("data", request));
		pair.add(new BasicNameValuePair("shandle", "0"));
		pair.add(new BasicNameValuePair("handle", "0"));
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST, pair);
		System.out.println(result);

		JsonObject jsonObject = JSONUtils.toJsonObject(result);
		JsonArray dateList = jsonObject.get("aaData").getAsJsonArray();

		StringBuilder sb = new StringBuilder("\r\n");
		for (JsonElement jsonElement : dateList) {
			
			NginxPVUVModel vo = JSONUtils.fromJsonNew(jsonElement, NginxPVUVModel.class);
			
			sb.append(vo.getName() + ",");
			sb.append(vo.getPrd_id() + ",");
			sb.append(vo.getUV() + ",");
			sb.append(vo.getPV() + ",");
			sb.append("\r\n");
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		
		String url = "http://xmiles.cn/stat_service/common";
//		String url = "http://127.0.0.1:8080/stat_service/common";

		JsonObject value = new JsonObject();
		JsonObject datebean = new JsonObject();
		value.addProperty("day", "2017-07-02");
		value.addProperty("interface_id", 480);
		value.addProperty("url", "HTTP");
		value.addProperty("table201_length", 25);
		datebean.add("databean", value);
		// 请求数据
		ArrayList<BasicNameValuePair> pair = new ArrayList<BasicNameValuePair>();
		pair.add(new BasicNameValuePair("data", datebean.toString()));
		pair.add(new BasicNameValuePair("shandle", "0"));
		pair.add(new BasicNameValuePair("handle", "0"));
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST, pair);
		LogService.info(result);
		System.out.println(result);
		JsonObject jsonObject = JSONUtils.toJsonObject(result);
		JsonArray dateList = jsonObject.get("aaData").getAsJsonArray();
		System.out.println(dateList.size());
		
	}

}
